
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>[k8s] 第二章 十分钟带你搭建k8s集群环境</title>
            </head>
            <body>
            <a href="https://andyoung.blog.csdn.net">原作者博客</a>
            <div id="content_views" class="markdown_views prism-atom-one-light">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <p>本章节主要介绍如何搭建kubernetes的集群环境</p> 
<h3><a id="_2"></a>环境规划</h3> 
<h4><a id="_4"></a>集群类型</h4> 
<p>kubernetes集群大体上分为两类：<strong>一主多从</strong>和<strong>多主多从</strong>。</p> 
<ul><li>一主多从：一台Master节点和多台Node节点，搭建简单，但是有单机故障风险，适合用于测试环境</li><li>多主多从：多台Master节点和多台Node节点，搭建麻烦，安全性高，适合用于生产环境</li></ul> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/9a87b06b78f18725fa57c03d9d8e8bfe.png" alt="image-20200404094800622"></p> 
<blockquote> 
 <p><code>说明：为了测试简单，本次搭建的是 一主两从 类型的集群</code></p> 
</blockquote> 
<h4><a id="_15"></a>安装方式</h4> 
<p>kubernetes有多种部署方式，目前主流的方式有kubeadm、minikube、二进制包</p> 
<ul><li>minikube：一个用于快速搭建单节点kubernetes的工具</li><li>kubeadm：一个用于快速搭建kubernetes集群的工具</li><li>二进制包 ：从官网下载每个组件的二进制包，依次去安装，此方式对于理解kubernetes组件更加有效</li></ul> 
<blockquote> 
 <p><code>说明：现在需要安装kubernetes的集群环境，但是又不想过于麻烦，所以选择使用kubeadm方式</code></p> 
</blockquote> 
<h4><a id="_25"></a>主机规划</h4> 
<table><thead><tr><th>作用</th><th>IP地址</th><th>操作系统</th><th>配置</th></tr></thead><tbody><tr><td>Master</td><td>192.168.109.101</td><td>Centos7.5 基础设施服务器</td><td>2颗CPU 2G内存 50G硬盘</td></tr><tr><td>Node1</td><td>192.168.109.102</td><td>Centos7.5 基础设施服务器</td><td>2颗CPU 2G内存 50G硬盘</td></tr><tr><td>Node2</td><td>192.168.109.103</td><td>Centos7.5 基础设施服务器</td><td>2颗CPU 2G内存 50G硬盘</td></tr></tbody></table> 
<h3><a id="_33"></a>环境搭建</h3> 
<p>​ 本次环境搭建需要安装三台Centos服务器（一主二从），然后在每台服务器中分别安装docker（18.06.3），kubeadm（1.17.4）、kubelet（1.17.4）、kubectl（1.17.4）程序。</p> 
<h4><a id="_37"></a>主机安装</h4> 
<p>安装虚拟机过程中注意下面选项的设置：</p> 
<ul><li> <p>操作系统环境：CPU（2C） 内存（2G） 硬盘（50G）</p> </li><li> <p>语言选择：中文简体</p> </li><li> <p>软件选择：基础设施服务器</p> </li><li> <p>分区选择：自动分区</p> </li><li> <p>网络配置：按照下面配置网路地址信息</p> <pre><code class="prism language-md">网络地址：192.168.109.100  （每台主机都不一样  分别为100、101、102）
子网掩码：255.255.255.0
默认网关：192.168.109.2
DNS：    223.5.5.5
</code></pre> <p><img src="https://i-blog.csdnimg.cn/blog_migrate/5c37084072b823dbec9695ab6498ce30.png" alt="image-20200505213817934"></p> </li><li> <p>主机名设置：按照下面信息设置主机名</p> <pre><code class="prism language-md">master节点： master
node节点：   node1
node节点：   node2
</code></pre> <p><img src="https://i-blog.csdnimg.cn/blog_migrate/05a95f95bce5a8ced46fc668b659cbdd.png" alt="image-20200505214156148"></p> </li></ul> 
<h4><a id="_70"></a>环境初始化</h4> 
<ol><li>检查操作系统的版本</li></ol> 
<pre><code class="prism language-powershell"><span class="token comment"># 此方式下安装kubernetes集群要求Centos版本要在7.5或之上</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># cat /etc/redhat-release</span>
CentOS Linux release 7<span class="token punctuation">.</span>5<span class="token punctuation">.</span>1804 <span class="token punctuation">(</span>Core<span class="token punctuation">)</span>
</code></pre> 
<p>2） 主机名解析</p> 
<p>为了方便后面集群节点间的直接调用，在这配置一下主机名解析，企业中推荐使用内部DNS服务器</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 主机名成解析 编辑三台服务器的/etc/hosts文件，添加下面内容</span>
192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>100  master
192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>101  node1
192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>102  node2
</code></pre> 
<p>3） 时间同步</p> 
<p>kubernetes要求集群中的节点时间必须精确一致，这里直接使用chronyd服务从网络同步时间。</p> 
<p>企业中建议配置内部的时<strong>间同步服务器</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 启动chronyd服务</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl start chronyd</span>
<span class="token comment"># 设置chronyd服务开机自启</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl enable chronyd</span>
<span class="token comment"># chronyd服务启动稍等几秒钟，就可以使用date命令验证时间了</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># date</span>
</code></pre> 
<p>4） 禁用iptables和firewalld服务</p> 
<p>kubernetes和docker在运行中会产生大量的iptables规则，为了不让系统规则跟它们混淆，直接关闭系统的规则</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 1 关闭firewalld服务</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl stop firewalld</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl disable firewalld</span>
<span class="token comment"># 2 关闭iptables服务</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl stop iptables</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl disable iptables</span>
</code></pre> 
<p>5） 禁用selinux</p> 
<p>selinux是linux系统下的一个安全服务，如果不关闭它，在安装集群中会产生各种各样的奇葩问题</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 编辑 /etc/selinux/config 文件，修改SELINUX的值为disabled</span>
<span class="token comment"># 注意修改完毕之后需要重启linux服务</span>
SELINUX=disabled
</code></pre> 
<p>6） 禁用swap分区</p> 
<p>swap分区指的是虚拟内存分区，它的作用是在物理内存使用完之后，将磁盘空间虚拟成内存来使用</p> 
<p>启用swap设备会对系统的性能产生非常负面的影响，因此kubernetes要求每个节点都要禁用swap设备</p> 
<p>但是如果因为某些原因确实不能关闭swap分区，就需要在集群安装过程中通过明确的参数进行配置说明</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 编辑分区配置文件/etc/fstab，注释掉swap分区一行</span>
<span class="token comment"># 注意修改完毕之后需要重启linux服务</span>
 UUID=455cc753<span class="token operator">-</span>7a60<span class="token operator">-</span>4c17<span class="token operator">-</span>a424<span class="token operator">-</span>7741728c44a1 <span class="token operator">/</span>boot    xfs     defaults        0 0
 <span class="token operator">/</span>dev<span class="token operator">/</span>mapper<span class="token operator">/</span>centos<span class="token operator">-</span>home <span class="token operator">/</span>home                      xfs     defaults        0 0
<span class="token comment"># /dev/mapper/centos-swap swap                      swap    defaults        0 0</span>
</code></pre> 
<p>7）修改linux的内核参数</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 修改linux的内核参数，添加网桥过滤和地址转发功能</span>
<span class="token comment"># 编辑/etc/sysctl.d/kubernetes.conf文件，添加如下配置:</span>
net<span class="token punctuation">.</span>bridge<span class="token punctuation">.</span>bridge<span class="token operator">-</span>nf<span class="token operator">-</span>call<span class="token operator">-</span>ip6tables = 1
net<span class="token punctuation">.</span>bridge<span class="token punctuation">.</span>bridge<span class="token operator">-</span>nf<span class="token operator">-</span>call<span class="token operator">-</span>iptables = 1
net<span class="token punctuation">.</span>ipv4<span class="token punctuation">.</span>ip_forward = 1

<span class="token comment"># 重新加载配置</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># sysctl -p</span>

<span class="token comment"># 加载网桥过滤模块</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># modprobe br_netfilter</span>

<span class="token comment"># 查看网桥过滤模块是否加载成功</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># lsmod | grep br_netfilter</span>
</code></pre> 
<p>8）配置ipvs功能</p> 
<p>在kubernetes中service有两种代理模型，一种是基于iptables的，一种是基于ipvs的</p> 
<p>两者比较的话，ipvs的性能明显要高一些，但是如果要使用它，需要手动载入ipvs模块</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 1 安装ipset和ipvsadm</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># yum install ipset ipvsadm -y</span>

<span class="token comment"># 2 添加需要加载的模块写入脚本文件</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># cat &lt;&lt;EOF &gt;  /etc/sysconfig/modules/ipvs.modules</span>
<span class="token comment">#!/bin/bash</span>
modprobe <span class="token operator">--</span> ip_vs
modprobe <span class="token operator">--</span> ip_vs_rr
modprobe <span class="token operator">--</span> ip_vs_wrr
modprobe <span class="token operator">--</span> ip_vs_sh
modprobe <span class="token operator">--</span> nf_conntrack_ipv4
EOF

<span class="token comment"># 3 为脚本文件添加执行权限</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># chmod +x /etc/sysconfig/modules/ipvs.modules</span>

<span class="token comment"># 4 执行脚本文件</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># /bin/bash /etc/sysconfig/modules/ipvs.modules</span>

<span class="token comment"># 5 查看对应的模块是否加载成功</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># lsmod | grep -e ip_vs -e nf_conntrack_ipv4</span>
</code></pre> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/64c97a13453eff625140e85152fa7b65.png" alt="image-20210826211133270"></p> 
<p>9） 重启服务器</p> 
<p>上面步骤完成之后，需要重新启动linux系统</p> 
<pre><code class="prism language-powershell"><span class="token namespace">[root@master ~]</span><span class="token comment"># reboot</span>
</code></pre> 
<h4><a id="docker_204"></a>安装docker</h4> 
<pre><code class="prism language-powershell"><span class="token comment"># 1 切换镜像源</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo</span>

<span class="token comment"># 2 查看当前镜像源中支持的docker版本</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># yum list docker-ce --showduplicates</span>

<span class="token comment"># 3 安装特定版本的docker-ce</span>
<span class="token comment"># 必须指定--setopt=obsoletes=0，否则yum会自动安装更高版本</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># yum install --setopt=obsoletes=0 docker-ce-18.06.3.ce-3.el7 -y</span>

<span class="token comment"># 4 添加一个配置文件</span>
<span class="token comment"># Docker在默认情况下使用的Cgroup Driver为cgroupfs，而kubernetes推荐使用systemd来代替cgroupfs</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># mkdir /etc/docker</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># cat &lt;&lt;EOF &gt;  /etc/docker/daemon.json</span>
<span class="token punctuation">{<!-- --></span>
  <span class="token string">"exec-opts"</span>: <span class="token punctuation">[</span><span class="token string">"native.cgroupdriver=systemd"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token string">"registry-mirrors"</span>: <span class="token punctuation">[</span><span class="token string">"https://kn0t2bca.mirror.aliyuncs.com"</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
EOF

<span class="token comment"># 5 启动docker</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl restart docker</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl enable docker</span>

<span class="token comment"># 6 检查docker状态和版本</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># docker version</span>
</code></pre> 
<h4><a id="kubernetes_235"></a>安装kubernetes组件</h4> 
<pre><code class="prism language-powershell"><span class="token comment"># 由于kubernetes的镜像源在国外，速度比较慢，这里切换成国内的镜像源</span>
<span class="token comment"># 编辑/etc/yum.repos.d/kubernetes.repo，添加下面的配置 </span>
<span class="token namespace">[kubernetes]</span>
name=Kubernetes
baseurl=http:<span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>aliyun<span class="token punctuation">.</span>com<span class="token operator">/</span>kubernetes<span class="token operator">/</span>yum<span class="token operator">/</span>repos<span class="token operator">/</span>kubernetes<span class="token operator">-</span>el7<span class="token operator">-</span>x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http:<span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>aliyun<span class="token punctuation">.</span>com<span class="token operator">/</span>kubernetes<span class="token operator">/</span>yum<span class="token operator">/</span>doc<span class="token operator">/</span>yum<span class="token operator">-</span>key<span class="token punctuation">.</span>gpg
       http:<span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>aliyun<span class="token punctuation">.</span>com<span class="token operator">/</span>kubernetes<span class="token operator">/</span>yum<span class="token operator">/</span>doc<span class="token operator">/</span>rpm<span class="token operator">-</span>package<span class="token operator">-</span>key<span class="token punctuation">.</span>gpg

<span class="token comment"># 安装kubeadm、kubelet和kubectl</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># yum install --setopt=obsoletes=0 kubeadm-1.17.4-0 kubelet-1.17.4-0 kubectl-1.17.4-0 -y</span>

<span class="token comment"># 配置kubelet的cgroup</span>
<span class="token comment"># 编辑/etc/sysconfig/kubelet，添加下面的配置</span>
KUBELET_CGROUP_ARGS=<span class="token string">"--cgroup-driver=systemd"</span>
KUBE_PROXY_MODE=<span class="token string">"ipvs"</span>

<span class="token comment"># 4 设置kubelet开机自启</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># systemctl enable kubelet</span>
</code></pre> 
<h4><a id="_261"></a>准备集群镜像</h4> 
<pre><code class="prism language-powershell"><span class="token comment"># 在安装kubernetes集群之前，必须要提前准备好集群需要的镜像，所需镜像可以通过下面命令查看</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubeadm config images list</span>

<span class="token comment"># 下载镜像</span>
<span class="token comment"># 此镜像在kubernetes的仓库中,由于网络原因,无法连接，下面提供了一种替代方案</span>
images=<span class="token punctuation">(</span>
    kube<span class="token operator">-</span>apiserver:v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
    kube<span class="token operator">-</span>controller<span class="token operator">-</span>manager:v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
    kube<span class="token operator">-</span>scheduler:v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
    kube<span class="token operator">-</span>proxy:v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
    pause:3<span class="token punctuation">.</span>1
    etcd:3<span class="token punctuation">.</span>4<span class="token punctuation">.</span>3<span class="token operator">-</span>0
    coredns:1<span class="token punctuation">.</span>6<span class="token punctuation">.</span>5
<span class="token punctuation">)</span>

<span class="token keyword">for</span> imageName in $<span class="token punctuation">{<!-- --></span>images<span class="token punctuation">[</span>@<span class="token punctuation">]</span><span class="token punctuation">}</span> <span class="token punctuation">;</span> <span class="token keyword">do</span>
	docker pull registry<span class="token punctuation">.</span>cn<span class="token operator">-</span>hangzhou<span class="token punctuation">.</span>aliyuncs<span class="token punctuation">.</span>com<span class="token operator">/</span>google_containers<span class="token operator">/</span><span class="token variable">$imageName</span>
	docker tag registry<span class="token punctuation">.</span>cn<span class="token operator">-</span>hangzhou<span class="token punctuation">.</span>aliyuncs<span class="token punctuation">.</span>com<span class="token operator">/</span>google_containers<span class="token operator">/</span><span class="token variable">$imageName</span> 		k8s<span class="token punctuation">.</span>gcr<span class="token punctuation">.</span>io<span class="token operator">/</span><span class="token variable">$imageName</span>
	docker rmi registry<span class="token punctuation">.</span>cn<span class="token operator">-</span>hangzhou<span class="token punctuation">.</span>aliyuncs<span class="token punctuation">.</span>com<span class="token operator">/</span>google_containers<span class="token operator">/</span><span class="token variable">$imageName</span>
done
</code></pre> 
<h4><a id="_286"></a>集群初始化</h4> 
<p>下面开始对集群进行初始化，并将node节点加入到集群中</p> 
<blockquote> 
 <p>下面的操作只需要在<code>master</code>(192.168.109.100 master ip)节点上执行即可</p> 
</blockquote> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建集群</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubeadm init \</span>
	<span class="token operator">--</span>kubernetes<span class="token operator">-</span>version=v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4 \
    <span class="token operator">--</span>pod<span class="token operator">-</span>network<span class="token operator">-</span>cidr=10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>0<span class="token punctuation">.</span>0<span class="token operator">/</span>16 \
    <span class="token operator">--</span>service<span class="token operator">-</span>cidr=10<span class="token punctuation">.</span>96<span class="token punctuation">.</span>0<span class="token punctuation">.</span>0<span class="token operator">/</span>12 \
    <span class="token operator">--</span>apiserver<span class="token operator">-</span>advertise<span class="token operator">-</span>address=192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>100

<span class="token comment"># 创建必要文件</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># mkdir -p $HOME/.kube</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># sudo chown $(id -u):$(id -g) $HOME/.kube/config</span>
</code></pre> 
<blockquote> 
 <p>下面的操作只需要在<code>node</code>节点上执行即可</p> 
</blockquote> 
<pre><code class="prism language-powershell"><span class="token comment"># 将node节点加入集群</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubeadm join 192.168.109.100:6443 \ </span>
	<span class="token operator">--</span>token 8507uc<span class="token punctuation">.</span>o0knircuri8etnw2 \
	<span class="token operator">--</span>discovery<span class="token operator">-</span>token<span class="token operator">-</span>ca<span class="token operator">-</span>cert<span class="token operator">-</span>hash \
	sha256:acc37967fb5b0acf39d7598f8a439cc7dc88f439a3f4d0c9cae88e7901b9d3f
	
<span class="token comment"># 查看集群状态 此时的集群状态为NotReady，这是因为还没有配置网络插件</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get nodes</span>
NAME     STATUS     ROLES    AGE     VERSION
master   NotReady   master   6m43s   v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
node1    NotReady   &lt;none&gt;   22s     v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
node2    NotReady   &lt;none&gt;   19s     v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
</code></pre> 
<h4><a id="_323"></a>安装网络插件</h4> 
<p>kubernetes支持多种网络插件，比如flannel、calico、canal等等，任选一种使用即可，本次选择flannel</p> 
<blockquote> 
 <p>下面操作依旧只在<code>master</code>节点执行即可，插件使用的是DaemonSet的控制器，它会在每个节点上都运行</p> 
</blockquote> 
<pre><code class="prism language-powershell"><span class="token comment"># 获取fannel的配置文件</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml</span>

<span class="token comment"># 修改文件中quay.io仓库为quay-mirror.qiniu.com</span>

<span class="token comment"># 使用配置文件启动fannel</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl apply -f kube-flannel.yml</span>

<span class="token comment"># 稍等片刻，再次查看集群节点的状态</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get nodes</span>
NAME     STATUS   ROLES    AGE     VERSION
master   Ready    master   15m     v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
node1    Ready    &lt;none&gt;   8m53s   v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
node2    Ready    &lt;none&gt;   8m50s   v1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>4
</code></pre> 
<p>至此，kubernetes的集群环境搭建完成</p> 
<h3><a id="_348"></a>服务部署</h3> 
<p>接下来在kubernetes集群中部署一个nginx程序，测试下集群是否在正常工作。</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 部署nginx</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create deployment nginx --image=nginx:1.14-alpine</span>

<span class="token comment"># 暴露端口</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl expose deployment nginx --port=80 --type=NodePort</span>

<span class="token comment"># 查看服务状态</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pods,service</span>
NAME                         READY   STATUS    RESTARTS   AGE
pod<span class="token operator">/</span>nginx<span class="token operator">-</span>86c57db685<span class="token operator">-</span>fdc2k   1<span class="token operator">/</span>1     Running   0          18m

NAME                 <span class="token function">TYPE</span>        CLUSTER<span class="token operator">-</span>IP      EXTERNAL<span class="token operator">-</span>IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>        AGE
service<span class="token operator">/</span>kubernetes   ClusterIP   10<span class="token punctuation">.</span>96<span class="token punctuation">.</span>0<span class="token punctuation">.</span>1       &lt;none&gt;        443<span class="token operator">/</span>TCP        82m
service<span class="token operator">/</span>nginx        NodePort    10<span class="token punctuation">.</span>104<span class="token punctuation">.</span>121<span class="token punctuation">.</span>45   &lt;none&gt;        80:30073<span class="token operator">/</span>TCP   17m

<span class="token comment"># 4 最后在电脑上访问下部署的nginx服务</span>
</code></pre> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/dffa685810cf17e668071562f75c885b.png" alt="image-20200405142656921"> 
<h2><a id="_374"></a>参考</h2> 
<p>https://kubernetes.io/zh/docs/tasks/tools/install-kubectl-linux/</p>
                </div>
            </body>
            </html>
            